using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace SubSonicResearch
{
	/// <summary>
	/// Strongly-typed collection for the Portal class.
	/// </summary>
    [Serializable]
	public partial class PortalCollection : ActiveList<Portal, PortalCollection>
	{	   
		public PortalCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>PortalCollection</returns>
		public PortalCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                Portal o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the Portals table.
	/// </summary>
	[Serializable]
	public partial class Portal : ActiveRecord<Portal>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Portal()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public Portal(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public Portal(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public Portal(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Portals", TableType.Table, DataService.GetInstance("ssDSN"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarPortalId = new TableSchema.TableColumn(schema);
				colvarPortalId.ColumnName = "PortalId";
				colvarPortalId.DataType = DbType.Int32;
				colvarPortalId.MaxLength = 0;
				colvarPortalId.AutoIncrement = true;
				colvarPortalId.IsNullable = false;
				colvarPortalId.IsPrimaryKey = true;
				colvarPortalId.IsForeignKey = false;
				colvarPortalId.IsReadOnly = false;
				colvarPortalId.DefaultSetting = @"";
				colvarPortalId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPortalId);
				
				TableSchema.TableColumn colvarPortalName = new TableSchema.TableColumn(schema);
				colvarPortalName.ColumnName = "PortalName";
				colvarPortalName.DataType = DbType.AnsiString;
				colvarPortalName.MaxLength = 50;
				colvarPortalName.AutoIncrement = false;
				colvarPortalName.IsNullable = false;
				colvarPortalName.IsPrimaryKey = false;
				colvarPortalName.IsForeignKey = false;
				colvarPortalName.IsReadOnly = false;
				colvarPortalName.DefaultSetting = @"";
				colvarPortalName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPortalName);
				
				TableSchema.TableColumn colvarAdminUserId = new TableSchema.TableColumn(schema);
				colvarAdminUserId.ColumnName = "AdminUserId";
				colvarAdminUserId.DataType = DbType.Int32;
				colvarAdminUserId.MaxLength = 0;
				colvarAdminUserId.AutoIncrement = false;
				colvarAdminUserId.IsNullable = false;
				colvarAdminUserId.IsPrimaryKey = false;
				colvarAdminUserId.IsForeignKey = true;
				colvarAdminUserId.IsReadOnly = false;
				colvarAdminUserId.DefaultSetting = @"";
				
					colvarAdminUserId.ForeignKeyTableName = "People";
				schema.Columns.Add(colvarAdminUserId);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ssDSN"].AddSchema("Portals",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("PortalId")]
		[Bindable(true)]
		public int PortalId 
		{
			get { return GetColumnValue<int>(Columns.PortalId); }
			set { SetColumnValue(Columns.PortalId, value); }
		}
		  
		[XmlAttribute("PortalName")]
		[Bindable(true)]
		public string PortalName 
		{
			get { return GetColumnValue<string>(Columns.PortalName); }
			set { SetColumnValue(Columns.PortalName, value); }
		}
		  
		[XmlAttribute("AdminUserId")]
		[Bindable(true)]
		public int AdminUserId 
		{
			get { return GetColumnValue<int>(Columns.AdminUserId); }
			set { SetColumnValue(Columns.AdminUserId, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		public SubSonicResearch.PortalPeopleXrefLCollection PortalPeopleXrefLs()
		{
			return new SubSonicResearch.PortalPeopleXrefLCollection().Where(PortalPeopleXrefL.Columns.PortalId, PortalId).Load();
		}
		public SubSonicResearch.PortalPeopleXrefCollection PortalPeopleXrefRecords()
		{
			return new SubSonicResearch.PortalPeopleXrefCollection().Where(PortalPeopleXref.Columns.PortalId, PortalId).Load();
		}
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Person ActiveRecord object related to this Portal
		/// 
		/// </summary>
		public SubSonicResearch.Person Person
		{
			get { return SubSonicResearch.Person.FetchByID(this.AdminUserId); }
			set { SetColumnValue("AdminUserId", value.PersonId); }
		}
		
		
		#endregion
		
		
		
		#region Many To Many Helpers
		
		 
		public SubSonicResearch.PersonCollection GetPersonCollection() { return Portal.GetPersonCollection(this.PortalId); }
		public static SubSonicResearch.PersonCollection GetPersonCollection(int varPortalId)
		{
		    SubSonic.QueryCommand cmd = new SubSonic.QueryCommand("SELECT * FROM [dbo].[People] INNER JOIN [PortalPeopleXref] ON [People].[PersonId] = [PortalPeopleXref].[PersonId] WHERE [PortalPeopleXref].[PortalId] = @PortalId", Portal.Schema.Provider.Name);
			cmd.AddParameter("@PortalId", varPortalId, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			PersonCollection coll = new PersonCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}
		
		public static void SavePersonMap(int varPortalId, PersonCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM [PortalPeopleXref] WHERE [PortalPeopleXref].[PortalId] = @PortalId", Portal.Schema.Provider.Name);
			cmdDel.AddParameter("@PortalId", varPortalId, DbType.Int32);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Person item in items)
			{
				PortalPeopleXref varPortalPeopleXref = new PortalPeopleXref();
				varPortalPeopleXref.SetColumnValue("PortalId", varPortalId);
				varPortalPeopleXref.SetColumnValue("PersonId", item.GetPrimaryKeyValue());
				varPortalPeopleXref.Save();
			}
		}
		public static void SavePersonMap(int varPortalId, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM [PortalPeopleXref] WHERE [PortalPeopleXref].[PortalId] = @PortalId", Portal.Schema.Provider.Name);
			cmdDel.AddParameter("@PortalId", varPortalId, DbType.Int32);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					PortalPeopleXref varPortalPeopleXref = new PortalPeopleXref();
					varPortalPeopleXref.SetColumnValue("PortalId", varPortalId);
					varPortalPeopleXref.SetColumnValue("PersonId", l.Value);
					varPortalPeopleXref.Save();
				}
			}
		}
		public static void SavePersonMap(int varPortalId , int[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM [PortalPeopleXref] WHERE [PortalPeopleXref].[PortalId] = @PortalId", Portal.Schema.Provider.Name);
			cmdDel.AddParameter("@PortalId", varPortalId, DbType.Int32);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (int item in itemList) 
			{
				PortalPeopleXref varPortalPeopleXref = new PortalPeopleXref();
				varPortalPeopleXref.SetColumnValue("PortalId", varPortalId);
				varPortalPeopleXref.SetColumnValue("PersonId", item);
				varPortalPeopleXref.Save();
			}
		}
		
		public static void DeletePersonMap(int varPortalId) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM [PortalPeopleXref] WHERE [PortalPeopleXref].[PortalId] = @PortalId", Portal.Schema.Provider.Name);
			cmdDel.AddParameter("@PortalId", varPortalId, DbType.Int32);
			DataService.ExecuteQuery(cmdDel);
		}
		
		#endregion
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varPortalName,int varAdminUserId)
		{
			Portal item = new Portal();
			
			item.PortalName = varPortalName;
			
			item.AdminUserId = varAdminUserId;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varPortalId,string varPortalName,int varAdminUserId)
		{
			Portal item = new Portal();
			
				item.PortalId = varPortalId;
			
				item.PortalName = varPortalName;
			
				item.AdminUserId = varAdminUserId;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn PortalIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn PortalNameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn AdminUserIdColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string PortalId = @"PortalId";
			 public static string PortalName = @"PortalName";
			 public static string AdminUserId = @"AdminUserId";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
}
        #endregion
	}
}
